//
//  UICollectionView+Extension.swift
//  EastNetWorkProject
//
//  Created by 伊伊 on 2023/12/25.
//

import Foundation
import UIKit
extension UICollectionView{
    convenience init(delegate:Any,_ custFlowOut:UICollectionViewLayout? = nil) {
     
        let flowOut = UICollectionViewFlowLayout()
        self.init(frame: .zero, collectionViewLayout: custFlowOut ?? flowOut)
        
    
        self.backgroundColor = .white
        self.isPrefetchingEnabled = false
        self.contentInsetAdjustmentBehavior = .never
        if let delegate = delegate as? UICollectionViewDelegate{
            self.delegate = delegate
        }
        if let datasource = delegate as? UICollectionViewDataSource{
            self.dataSource = datasource
        }
    }
    
    func registerCells<T:UICollectionViewCell>(cellClasses:[T.Type]){
        cellClasses.forEach { cellType in
            self.register(cellClass: cellType)
        }
    }
    
    
    
    func register<T:UICollectionViewCell>(cellClass: T.Type){
        self.register(cellClass, forCellWithReuseIdentifier: NSStringFromClass(cellClass))
    }
    func dequeueReusableCell<T:UICollectionViewCell>(withCellClass cellClass:T.Type, for indexPath: IndexPath) -> T{
        let cell = self.dequeueReusableCell(withReuseIdentifier: NSStringFromClass(cellClass), for: indexPath) as! T
        return cell
    }
    
    
    
    func register<T:UICollectionReusableView>(headerClass: T.Type){
        self.register(headerClass, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: NSStringFromClass(headerClass))
    }
    
    func dequeueReusableView<T:UICollectionReusableView>(headViewClass:T.Type, for indexPath: IndexPath) -> T{
        let headView = self.dequeueReusableSupplementaryView(ofKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: NSStringFromClass(headViewClass), for: indexPath)
        return headView as! T
    }
 
    func register<T:UICollectionReusableView>(footerClass: T.Type){
        self.register(footerClass, forSupplementaryViewOfKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: NSStringFromClass(footerClass))
    }
    
    func dequeueReusableView<T:UICollectionViewCell>(footerViewClass:T.Type, for indexPath: IndexPath) -> T{
        let footerView = self.dequeueReusableSupplementaryView(ofKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: NSStringFromClass(footerViewClass), for: indexPath)
        return footerView as! T
    }
    
    
    
}
